/**************************************************************
  > File Name: setjmp.c
  > Author: LuoLiang
  > Mail: 1204553475@qq.com 
  > Created Time: 2022年01月03日 星期一 16时42分32秒
 **************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>

/*main->a->b->c->d
 *
 *
 */

static jmp_buf g_env;
static void d()
{
	printf("%s():Begin.\n",__FUNCTION__);
	printf("%s():Jump now!\n",__FUNCTION__);
	longjmp(g_env,2);
	printf("%s():End.\n",__FUNCTION__);
}

static void c()
{
	printf("%s():Begin.\n",__FUNCTION__);
	printf("%s():Call d().\n",__FUNCTION__);
	d();
	printf("%s():d() returned.\n",__FUNCTION__);
	printf("%s():End.\n",__FUNCTION__);
}

static void b()
{
	printf("%s():Begin.\n",__FUNCTION__);
	printf("%s():Call c().\n",__FUNCTION__);
	c();
	printf("%s():c() returned.\n",__FUNCTION__);
	printf("%s():End.\n",__FUNCTION__);
}

static void a()
{
	int ret;
	printf("%s():Begin.\n",__FUNCTION__);
	ret = setjmp(g_env);
	if (ret == 0)
	{
		printf("%s():Call b().\n",__FUNCTION__);
		b();
		printf("%s():b() returned.\n",__FUNCTION__);
	}
	else
	{
		printf("%s():Jumped back here  with code: %d\n",__FUNCTION__,ret);
	}
	printf("%s():End.\n",__FUNCTION__);
}


int main(int argc,char **argv)
{
	printf("%s():Begin.\n",__FUNCTION__);
	printf("%s():Call a().\n",__FUNCTION__);
	a();
	printf("%s():a() returned.\n",__FUNCTION__);
	printf("%s():End.\n",__FUNCTION__);

    exit(0);
}
